{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "83dea728",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 创建向量\n",
    "import numpy as np\n",
    "vector_row = np.array([1, 2, 3])\n",
    "vector_col = np.array([[1], [2], [3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3dfe76e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 创建矩阵\n",
    "matrix = np.array([\n",
    "    [1, 2],\n",
    "    [1, 2],\n",
    "    [1, 2]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "288bfa88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  (1, 1)\t1\n",
      "  (2, 1)\t3\n"
     ]
    }
   ],
   "source": [
    "## 创建稀疏矩阵\n",
    "from scipy import sparse\n",
    "matrix = np.array([\n",
    "    [0, 0],\n",
    "    [0, 1],\n",
    "    [0, 3]\n",
    "])\n",
    "matrix_sparse = sparse.csc_matrix(matrix)\n",
    "print(matrix_sparse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1f61baed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "## 选择元素\n",
    "vector = np.array([1, 2, 3, 4, 5])\n",
    "matrix = np.array([\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6],\n",
    "    [7, 8, 8]\n",
    "])\n",
    "print(vector[2])\n",
    "print(matrix[0][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5d933dc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "[3 6 8]\n"
     ]
    }
   ],
   "source": [
    "## 切片\n",
    "print(vector[:])\n",
    "print(matrix[:, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fe6df3fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shape= (3, 3)\n",
      "size= 9\n",
      "ndim= 2\n"
     ]
    }
   ],
   "source": [
    "## 矩阵的属性\n",
    "matrix = np.array([\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6],\n",
    "    [7, 8, 8]\n",
    "])\n",
    "print(\"shape=\", matrix.shape)  ## 形状\n",
    "print(\"size=\", matrix.size)    ## 元素数量\n",
    "print(\"ndim=\", matrix.ndim)      ## 维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e8a8510c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[101, 102, 103],\n",
       "       [104, 105, 106],\n",
       "       [107, 108, 108]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 多个元素应用某种操作\n",
    "matrix = np.array([\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6],\n",
    "    [7, 8, 8]\n",
    "])\n",
    "add_100 = lambda a: a+100\n",
    "# 向量化函数\n",
    "vectorized_add100 = np.vectorize(add_100)\n",
    "vectorized_add100(matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9c0a2300",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9 1\n"
     ]
    }
   ],
   "source": [
    "## 一些函数\n",
    "matrix = np.array([\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6],\n",
    "    [7, 8, 9]\n",
    "])\n",
    "## 最大值 最小值\n",
    "print(np.max(matrix), np.min(matrix))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "14633006",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7 8 9]\n",
      "[3 6 9]\n"
     ]
    }
   ],
   "source": [
    "## 找到每一列的最大值\n",
    "print(np.max(matrix, axis=0))\n",
    "## 找到每一行的最大值\n",
    "print(np.max(matrix, axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8b435f59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5.0, 6.666666666666667, 2.581988897471611)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 平均值 方差 标准差\n",
    "np.mean(matrix), np.var(matrix), np.std(matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "bd827265",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  3  4  5  6  7  8  9 10 11 12]\n",
      "(12,)\n"
     ]
    }
   ],
   "source": [
    "## 矩阵变形\n",
    "matrix = np.arange(1, 13)\n",
    "print(matrix)\n",
    "print(matrix.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1562a8a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4]\n",
      " [ 5  6  7  8]\n",
      " [ 9 10 11 12]]\n",
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "matrix = matrix.reshape(3, 4)\n",
    "print(matrix)\n",
    "print(matrix.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ca1a96c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4 7]\n",
      " [2 5 8]\n",
      " [3 6 9]]\n"
     ]
    }
   ],
   "source": [
    "## 转置\n",
    "matrix = np.array([\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6],\n",
    "    [7, 8, 9]\n",
    "])\n",
    "print(matrix.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "29783378",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]]\n",
      "[[1]\n",
      " [2]\n",
      " [3]]\n"
     ]
    }
   ],
   "source": [
    "## 行=>列\n",
    "print(np.array([[1, 2, 3]]))\n",
    "print(np.array([[1, 2, 3]]).T)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b24c7c72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "## 秩\n",
    "matrix = np.array([\n",
    "    [1, 1, 1],\n",
    "    [1, 1, 10],\n",
    "    [1, 1, 15]\n",
    "])\n",
    "print(np.linalg.matrix_rank(matrix))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "bb957230",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n"
     ]
    }
   ],
   "source": [
    "## 行列式\n",
    "matrix = np.array([\n",
    "    [1, 2, 3],\n",
    "    [2, 4, 6],\n",
    "    [3, 8, 9]\n",
    "])\n",
    "print(np.linalg.det(matrix))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "70154c5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 5 9]\n"
     ]
    }
   ],
   "source": [
    "## 获取对角线元素\n",
    "matrix = np.array([\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6],\n",
    "    [7, 8, 9]\n",
    "])\n",
    "print(matrix.diagonal())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "433f5e49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "## 迹\n",
    "matrix = np.array([\n",
    "    [1, 2, 3],\n",
    "    [2, 4, 6],\n",
    "    [3, 8, 9]\n",
    "])\n",
    "print(matrix.trace())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b511b955",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[13.55075847  0.74003145 -3.29078992]\n",
      "[[-0.17622017 -0.96677403 -0.53373322]\n",
      " [-0.435951    0.2053623  -0.64324848]\n",
      " [-0.88254925  0.15223105  0.54896288]]\n"
     ]
    }
   ],
   "source": [
    "## 特征值和特征向量\n",
    "matrix = np.array([\n",
    "    [1, -1, 3],\n",
    "    [1, 1, 6],\n",
    "    [3, 8, 9]\n",
    "])\n",
    "eigenvalues, eigenvectors = np.linalg.eig(matrix)\n",
    "print(eigenvalues)\n",
    "print(eigenvectors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3103d7db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "32\n",
      "32\n"
     ]
    }
   ],
   "source": [
    "## 点积\n",
    "vector_a = np.array([1, 2, 3])\n",
    "vector_b = np.array([4, 5, 6])\n",
    "a_dot_b = np.dot(vector_a, vector_b)\n",
    "print(a_dot_b)\n",
    "print(vector_a @ vector_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a021e95e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  4  2]\n",
      " [ 2  4  2]\n",
      " [ 2  4 10]]\n",
      "[[ 0 -2  0]\n",
      " [ 0 -2  0]\n",
      " [ 0 -2 -6]]\n",
      "[[ 2  4  2]\n",
      " [ 2  4  2]\n",
      " [ 2  4 10]]\n",
      "[[ 0 -2  0]\n",
      " [ 0 -2  0]\n",
      " [ 0 -2 -6]]\n"
     ]
    }
   ],
   "source": [
    "## 矩阵加减\n",
    "matrix_a = np.array([\n",
    "    [1, 1, 1],\n",
    "    [1, 1, 1],\n",
    "    [1, 1, 2]\n",
    "]) \n",
    "matrix_b = np.array([\n",
    "    [1, 3, 1],\n",
    "    [1, 3, 1],\n",
    "    [1, 3, 8]\n",
    "])\n",
    "print(np.add(matrix_a, matrix_b))\n",
    "print(np.subtract(matrix_a, matrix_b))\n",
    "\n",
    "print(matrix_a + matrix_b)\n",
    "print(matrix_a - matrix_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "cbdc90a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 5]\n",
      " [3 7]]\n",
      "[[2 5]\n",
      " [3 7]]\n"
     ]
    }
   ],
   "source": [
    "## 矩阵乘法\n",
    "matrix_a = np.array([\n",
    "    [1, 1],\n",
    "    [1, 2]\n",
    "])\n",
    "matrix_b = np.array([\n",
    "    [1, 3],\n",
    "    [1, 2]\n",
    "])\n",
    "print(np.dot(matrix_a, matrix_b))\n",
    "print(matrix_a @ matrix_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "85c7aca4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 3]\n",
      " [1 4]]\n"
     ]
    }
   ],
   "source": [
    "## 对应位置相乘\n",
    "print(matrix_a * matrix_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "0816c249",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.66666667  1.33333333]\n",
      " [ 0.66666667 -0.33333333]]\n",
      "[[1. 0.]\n",
      " [0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "## 矩阵的逆\n",
    "matrix = np.array([\n",
    "    [1, 4],\n",
    "    [2, 5]\n",
    "])\n",
    "print(np.linalg.inv(matrix))\n",
    "\n",
    "## A @ A^-1 = I\n",
    "print(matrix @ np.linalg.inv(matrix))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "83f6ca94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.15416284 0.7400497  0.26331502]\n",
      "[3 3 0]\n",
      "[-0.42069204 -0.7060276   0.58535808]\n",
      "[ 1.75621946 -6.09046788  0.08495517]\n",
      "[1.55203763 1.48537741 1.76813415]\n"
     ]
    }
   ],
   "source": [
    "## 生成随机数\n",
    "np.random.seed(12)          ## 设置随机种子\n",
    "print(np.random.random(3))              ## 生成3个0.0~1.0之间的随机浮点数\n",
    "print(np.random.randint(0, 11, 3))      ## 生成3个0~11之间的随机整数\n",
    "print(np.random.normal(0.0, 1.0, 3))    ## 生成3个数 从平均值0.0标准差1.0的正态分布中取\n",
    "print(np.random.logistic(0.0, 1.0, 3))  ## 生成3个数 从平均值0.0散布程度1.0的logistic分布中取\n",
    "print(np.random.uniform(1.0, 2.0, 3))   ## 生成3个数 从[1.0, 2.0)范围内取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c30980fb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
